statistic = {}
statistic.actionId = '';
statistic.level = 0;
statistic.list = function () {
    main.init();
	$("#content").html(fly.template('http://naima.vn/statistic/list.html', {
		data: null,
	}));
	statistic.load('order');
	datepicker.init();
}
statistic.listCat;
statistic.loadColCategory = function(actionId){
	$('.header-menu-active').removeClass('header-menu-active');
	$('.categorybrand').attr('class','header-menu-active new-icon category');
	if(!actionId)
		actionId = statistic.actionId;
	statistic.actionId = actionId;
	fly.ajax({
        service:'/statistic/loadColCategory',
        data:{startDate : $('#fromTime').val(), endDate: $('#toTime').val(), actionId: actionId, categoryId : $('.categorySelect').val() ,pageView : $('#sl-page-size .totalviewactive').attr('num')},
        success:function (result) {			
			$(".datacontent").html(fly.template('http://naima.vn/statistic/category.html', {
				data: result.data,
			}));
			statistic.listCat = result.data['categories'];
			statistic.printCat(0);
			$('.categorySelect').val(result.data['categoryId']);
			$('.fill').attr('onclick','statistic.loadColCategory()');
			$('#sl-page-size a').unbind('click');
			$('#sl-page-size a').click(function(){
				$('#sl-page-size .totalviewactive').removeClass('totalviewactive');
				$(this).parent().attr('class','totalviewactive');
				statistic.loadColCategory();
			});
            if (result.status) {
				var tmp = new Array();
				var i = 0;
				$.each(result.data['data'], function(){
					if(this['value'] != null){
						var _x = this['date'];
						var _y = this['value'];					
						tmp[i] = new Array(_x,parseInt(_y));						
						i++;
					}
				});
				if(tmp.length > 3)
					statistic.loadOrderLine(tmp);
				else
					alert('Không có dữ liệu');
				$.each($('input[name=actionType]'), function(){
					if($(this).val() == actionId){
						$(this).attr('checked', 'checked');
					}
				});
            } else {
                popup.msg(result.message);
            }
        }
    });	
}
statistic.printCat = function(parentId){
    $.each(statistic.listCat, function(){
        var cat = this;
        if(cat.parentId == parentId){
            statistic.level ++;
            var tmp = "";
            var i = statistic.level;
            while(i){
                tmp += "- - ";
                i--;
            }
            
            $('.categorySelect').append('<option value="'+cat.id+'">'+tmp+cat.name+'</option>');
            statistic.printCat(cat.id);
            statistic.level --;
        }
    });
    
}

statistic.loadCategory = function(actionId){	
	$('.header-menu-active').removeClass('header-menu-active');
	$('.category').attr('class','header-menu-active new-icon category');
	if(!actionId)
	actionId = statistic.actionId;
	statistic.actionId = actionId;
	fly.ajax({
        service:'/statistic/loadCategory',
        data:{startDate : $('#fromTime').val(), endDate: $('#toTime').val(), actionId: actionId, brandId : $('.brandSelect').val() ,pageView : $('#sl-page-size .totalviewactive').attr('num')},
        success:function (result) {			
			$(".datacontent").html(fly.template('http://naima.vn/statistic/pie-category.html', {
				data: result.data,
			}));
			$('.fill').attr('onclick','statistic.loadCategory()');
			$('#sl-page-size a').unbind('click');
			$('#sl-page-size a').click(function(){
				$('#sl-page-size .totalviewactive').removeClass('totalviewactive');
				$(this).parent().attr('class','totalviewactive');
				statistic.loadCategory();
			});
            if (result.status) {
				var tmp = new Array();
				$.each(result.data['data'], function(i){
					$('._piedata').append('<tr>\
												<td>'+this['brandName']+'</td>\
												<td>'+parseInt(this['GMV']).toMoney(0,',','.')+'</td>\
												<td>'+parseInt(this['quantity'])+'</td>\
												<td>'+parseInt(this['GMV']/this['quantity']).toMoney(0,',','.')+'</td>\
											</tr>');
					var _x = this['brandName'];
					var _y = this['value'];
					if(_x && _y)
						tmp[i] = new Array(_x,parseInt(_y));
				});
				statistic.buildBrandPieChart(tmp);
				
				$.each($('input[name=actionType]'), function(){
					if($(this).val() == actionId){
						$(this).attr('checked', 'checked');
					}
				});
            } else {
                popup.msg(result.message);
            }
        }
    });	
}

statistic.loadBuyer = function(actionId){
	$('.header-menu-active').removeClass('header-menu-active');
	$('.buyer').attr('class','header-menu-active new-icon buyer');
	if(!actionId)
		actionId = statistic.actionId;
	statistic.actionId = actionId;
	fly.ajax({
        service:'/statistic/listBuyer',
        data:{startDate : $('#fromTime').val(), endDate: $('#toTime').val(), actionId: actionId, brandId : $('.brandSelect').val() ,pageView : $('#sl-page-size .totalviewactive').attr('num')},
        success:function (result) {			
			$(".datacontent").html(fly.template('http://naima.vn/statistic/buyer.html', {
				data: result.data,
			}));
			$('.fill').attr('onclick','statistic.loadBuyer()');
			$('#sl-page-size a').unbind('click');
			$('#sl-page-size a').click(function(){
				$('#sl-page-size .totalviewactive').removeClass('totalviewactive');
				$(this).parent().attr('class','totalviewactive');
				statistic.loadBuyer();
			});
            if (result.status) {
				var tmp = new Array();
				var i = 0;
				$.each(result.data['data'], function(){
					$('._piedata').append('<tr>\
												<td>'+this['buyerName']+'</td>\
												<td>'+parseInt(this['GMV']).toMoney(0,',','.')+'</td>\
												<td>'+parseInt(this['quantity'])+'</td>\
												<td>'+parseInt(this['GMV']/this['quantity']).toMoney(0,',','.')+'</td>\
											</tr>');
					var _x = this['buyerName'];
					var _y = this['value'];
					if(_x && _y){
						tmp[i] = new Array(_x,parseInt(_y));
						i++;
					}
				});
				statistic.buildBrandPieChart(tmp);
				$.each($('input[name=actionType]'), function(){
					if($(this).val() == actionId){
						$(this).attr('checked', 'checked');
					}
				});
            } else {
                popup.msg(result.message);
            }
        }
    });	
}

statistic.loadSeller = function(actionId){
	$('.header-menu-active').removeClass('header-menu-active');
	$('.seller').attr('class','header-menu-active new-icon seller');
	if(!actionId)
		actionId = statistic.actionId;
	statistic.actionId = actionId;
	fly.ajax({
        service:'/statistic/listSeller',
        data:{startDate : $('#fromTime').val(), endDate: $('#toTime').val(), actionId: actionId, brandId : $('.brandSelect').val() ,pageView : $('#sl-page-size .totalviewactive').attr('num')},
        success:function (result) {			
			$(".datacontent").html(fly.template('http://naima.vn/statistic/seller.html', {
				data: result.data,
			}));
			$('.fill').attr('onclick','statistic.loadSeller()');
			$('#sl-page-size a').unbind('click');
			$('#sl-page-size a').click(function(){
				$('#sl-page-size .totalviewactive').removeClass('totalviewactive');
				$(this).parent().attr('class','totalviewactive');
				statistic.loadSeller();
			});
            if (result.status) {
				var tmp = new Array();
				var i = 0;
				$.each(result.data['data'], function(){
					$('._piedata').append('<tr>\
												<td>'+this['sellerName']+'</td>\
												<td>'+parseInt(this['GMV']).toMoney(0,',','.')+'</td>\
												<td>'+parseInt(this['quantity'])+'</td>\
												<td>'+parseInt(this['GMV']/this['quantity']).toMoney(0,',','.')+'</td>\
											</tr>');
					var _x = this['sellerName'];
					var _y = this['value'];
					if(_x && _y){
						tmp[i] = new Array(_x,parseInt(_y));
						i++;
					}
				});
				statistic.buildBrandPieChart(tmp);
				$.each($('input[name=actionType]'), function(){
					if($(this).val() == actionId){
						$(this).attr('checked', 'checked');
					}
				});
            } else {
                popup.msg(result.message);
            }
        }
    });	
}

statistic.loadPieBrand = function(actionId){	
	$('.header-menu-active').removeClass('header-menu-active');
	$('.brand').attr('class','header-menu-active new-icon brand');
	if(!actionId)
	actionId = statistic.actionId;
	statistic.actionId = actionId;
	fly.ajax({
        service:'/statistic/pieChartBrand',
        data:{startDate : $('#fromTime').val(), endDate: $('#toTime').val(), actionId: actionId, brandId : $('.brandSelect').val() ,pageView : $('#sl-page-size .totalviewactive').attr('num')},
        success:function (result) {			
			$(".datacontent").html(fly.template('http://naima.vn/statistic/pie-brand.html', {
				data: result.data,
			}));
			$('.fill').attr('onclick','statistic.loadPieBrand()');
			$('#sl-page-size a').unbind('click');
			$('#sl-page-size a').click(function(){
				$('#sl-page-size .totalviewactive').removeClass('totalviewactive');
				$(this).parent().attr('class','totalviewactive');
				statistic.loadPieBrand();
			});
            if (result.status) {
				var tmp = new Array();
				$.each(result.data['data'], function(i){
					$('._piedata').append('<tr>\
												<td>'+this['brandName']+'</td>\
												<td>'+parseInt(this['GMV']).toMoney(0,',','.')+'</td>\
												<td>'+parseInt(this['quantity'])+'</td>\
												<td>'+parseInt(this['GMV']/this['quantity']).toMoney(0,',','.')+'</td>\
											</tr>');
					var _x = this['brandName'];
					var _y = this['value'];
					if(_x && _y)
						tmp[i] = new Array(_x,parseInt(_y));
				});
				statistic.buildBrandPieChart(tmp);
				
				$.each($('input[name=actionType]'), function(){
					if($(this).val() == actionId){
						$(this).attr('checked', 'checked');
					}
				});
            } else {
                popup.msg(result.message);
            }
        }
    });	
}

statistic.loadBrand = function(actionId){	
	$('.header-menu-active').removeClass('header-menu-active');
	$('.brand').attr('class','header-menu-active new-icon brand');
	if(!actionId)
		actionId = statistic.actionId;
	statistic.actionId = actionId;
	fly.ajax({
        service:'/statistic/listBrand',
        data:{startDate : $('#fromTime').val(), endDate: $('#toTime').val(), actionId: actionId, brandId : $('.brandSelect').val() ,pageView : $('#sl-page-size .totalviewactive').attr('num')},
        success:function (result) {			
			$(".datacontent").html(fly.template('http://naima.vn/statistic/brand.html', {
				data: result.data,
			}));
			$.each(result.data['brands'], function(){
				$('.brandSelect').append('<option value="'+this.id+'">'+this.name+'</option>');
			});
			$('.brandSelect').val(result.data['brandId']);
			$('.fill').attr('onclick','statistic.loadBrand()');
			$('#sl-page-size a').unbind('click');
			$('#sl-page-size a').click(function(){
				$('#sl-page-size .totalviewactive').removeClass('totalviewactive');
				$(this).parent().attr('class','totalviewactive');
				statistic.loadBrand();
			});
            if (result.status) {
				var tmp = new Array();
				var i = 0;
				$.each(result.data['data'], function(){
					var _x = this['date'];
					var _y = this['value'];
					if(_x && _y){
						tmp[i] = new Array(_x,parseInt(_y));
						i++;
					}
				});
				statistic.loadOrderLine(tmp);
				$.each($('input[name=actionType]'), function(){
					if($(this).val() == actionId){
						$(this).attr('checked', 'checked');
					}
				});
            } else {
                popup.msg(result.message);
            }
        }
    });	
}

statistic.load = function(actionId){	
	$('.header-menu-active').removeClass('header-menu-active');
	$('.order').attr('class','header-menu-active new-icon order');
	if(!actionId)
		actionId = statistic.actionId;
	statistic.actionId = actionId;
	fly.ajax({
        service:'/statistic/list',
        data:{startDate : $('#fromTime').val(), endDate: $('#toTime').val(), actionId: actionId, pageView : $('#sl-page-size .totalviewactive').attr('num')},
        success:function (result) {			
			$(".datacontent").html(fly.template('http://naima.vn/statistic/order.html', {
				data: result.data,
			}));
			$('.fill').attr('onclick','statistic.load()');
			$('#sl-page-size a').unbind('click');
			$('#sl-page-size a').click(function(){
				$('#sl-page-size .totalviewactive').removeClass('totalviewactive');
				$(this).parent().attr('class','totalviewactive');
				statistic.load();
			});
            if (result.status) {
				var tmp = new Array();
				var i = 0;
				$.each(result.data['data'], function(){
					var _x = this['date'];
					var _y = this['value'];
					if(_x && _y){
						tmp[i] = new Array(_x,parseInt(_y));
						i++;
					}
				});
				statistic.loadOrderLine(tmp);
				$.each($('input[name=actionType]'), function(){
					if($(this).val() == actionId){
						$(this).attr('checked', 'checked');
					}
				});
            } else {
                popup.msg(result.message);
            }
        }
    });	
}

statistic.buildBrandPieChart = function(data){
	var colors = ['#FFCC00', '#FFFF00', '#CCFF00', '#99FF00', '#33FF00', '#00FF66', '#00FF99', '#00FFCC', '#FF0000', '#FF3300', '#FF6600', '#FF9900'];
	var myChart = new JSChart('graph', 'pie');
	var _colors = [];
	for(i=0;i<data.length;i++){
		_colors[i] = colors[i];
	}
	myChart.setDataArray(data);
	myChart.colorizePie(_colors);
	myChart.setPiePosition(240, 170);
	myChart.setPieRadius(120);
	myChart.setPieUnitsFontSize(8);
	myChart.setPieUnitsColor('#474747');
	myChart.setPieValuesColor('#474747');
	myChart.setPieValuesOffset(-10);
	myChart.setTitleColor('#fff');
	myChart.setSize(580, 421);
	//myChart.setBackgroundImage('chart_bg.jpg'/*tpa=http://naima.vn/assets/9a4a72bc/js/order/chart_bg.jpg*/);
	myChart.draw();
}

statistic.loadOrderLine = function(data){
	var myChart = new JSChart('graph', 'line');
	myChart.setDataArray(data);
	myChart.setTitle('Thống kê');
	myChart.setTitleColor('#8E8E8E');
	myChart.setTitleFontSize(11);
	myChart.setAxisNameX('');
	myChart.setAxisNameY('');
	myChart.setAxisColor('#8420CA');
	myChart.setAxisValuesColor('#949494');
	myChart.setAxisPaddingLeft(70);
	myChart.setAxisPaddingRight(20);
	myChart.setAxisPaddingTop(50);
	myChart.setAxisPaddingBottom(40);
	myChart.setAxisValuesDecimals(0);
	myChart.setAxisValuesNumberX(10);
	myChart.setShowXValues(false);
	myChart.setGridColor('#C5A2DE');
	myChart.setLineColor('#BBBBBB');
	myChart.setLineWidth(2);
	myChart.setFlagColor('#9D12FD');
	myChart.setFlagRadius(4);
	$.each(data, function(){
		myChart.setTooltip([this[0], this[1].toMoney(0,',','.')]);		
		//myChart.setLabelX([this[0], this[0]]);
	});
	myChart.setSize(970, 400);
	//myChart.setBackgroundImage('chart_bg.jpg'/*tpa=http://naima.vn/assets/9a4a72bc/js/order/chart_bg.jpg*/);
	myChart.draw();
}
